////////////////////////////////// MESSAGES BOX PLUGIN //////////////////////////////////
(function( $ ){
	var _box = null;
	 var methods = {
	    init : init,
	    add : add,
	    clear : clear,
	    show : function( ) { alert("Soy show!"); },
	    hide : function( ) { alert("Soy hide!"); },
	    update : function() { alert("Soy update!"); },
	  };

	  $.fn.msgBox  = function( method ) {
		  _box = $(this);
	    // Method calling logic
	    if ( methods[method] ) {
	      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } else if ( typeof method === 'object' || ! method ) {
	      return methods.init.apply( this, arguments );
	    } else {
	      $.error( 'Method ' +  method + ' does not exist on jQuery.msgBox' );
	    }  
	  };
		  	  
	  function init( options ) {
		_box.append($("<p></p>", {html: "This is the box for messages"}));
		return _box;
	  }

	  function add(message) {
		  _box.html('');
		  var type = message.type;
		  var content = message.msg;
		  var element = $("<p></p>", {html: content});
		  
		  if("error" == type) element.addClass("error");
		  if("warning" == type) element.addClass("warning");
		  if("success" == type) element.addClass("success");
		  
		  _box.append(element);
	  }

	  function clear() {
		  _box.html('');
	  }
		
})( jQuery );
////////////////////////////////// END MESSAGES BOX PLUGIN //////////////////////////////////
////////////////////////////////// ACTIONS BOX PLUGIN //////////////////////////////////
(function( $ ){
	var _box = null;
	var methods = {
			init : init,
			add : add,
			
	};

	$.fn.actionsBox = function( method ) {
		 _box = $(this);
	    // Method calling logic
	    if ( methods[method] ) {
	      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } else if ( typeof method === 'object' || ! method ) {
	      return methods.init.apply( this, arguments );
	    } else {
	      $.error( 'Method ' +  method + ' does not exist on jQuery.actionsBox' );
	    }    

	    return _box;
	};

	function init() {
	}
	
	function add(actions) {
		$(actions).each(function(i, action){
			var actionElement = $("<p></p>", {id : action.id, html: action.title}).bind('click', action.fnClick);	
			_box.append(actionElement);
		});
	}
	
})( jQuery );
//////////////////////////////////END ACTIONS BOX PLUGIN //////////////////////////////////
////////////////////////////////// MODAL FORM PLUGIN //////////////////////////////////
(function( $ ){
	$.fn.modalform = function( options ) {
		var $this = $(this);
		$this.data('init', false);
		
		var settings = {
			ajaxForm: '', // The ajax uri for render the form.
			msgId: 'form-messages', // The messages id div.
			fnClose: '', // Callback function that will be executed when the dialog is closed. Useful for update some data like a table.
			 
		};

		if( options ) {
			$.extend(settings, options);
		}

		// Create the dialog
		var div = $('<div></div>');
		var $dialog = div.dialog({
			autoOpen: false,
			modal: true,
			title: $this.attr('title'),
		});

		// Clear old messages
		var messagesDiv = $("#"+settings.msgId).html('');
		
		// Start
		if(!($this.data('init'))) {
			// Get the form.
			$.get(settings.ajaxForm, function(data){
				// Append the form to the div that is the dialog
				div.append(data);
				// Getting the form and convert it into ajaxForm.
				var form = $(div).find('form');
				if(form) $this.data('init', true);
				form.ajaxForm(function(data){
					var messagesDiv = $("#"+settings.msgId);
					if(data.errors) {
						$.each(data.errors, function(key, val){
							form.find("[name='"+key+"']").each(function(){
								$(this).next().html(val);
							});
						});
					} else {
						if(messagesDiv) {
							messagesDiv.html(data.message);
							$dialog.dialog('close');
							//$dialog.dialog('destroy');
							div.html('');
							$this.data('init', false);
							if(settings.fnClose) {
								settings.fnClose(true);
							}
						}
					}
				});
				
			});
		}
		$dialog.dialog('open');
	};
})( jQuery );
////////////////////////////////// END MODAL FORM PLUGIN //////////////////////////////////